# coding:utf-8
import json


class ResponseMiddleware:
    """
    修改 Response Content 例如code, requestId, msg等
    """
    def process_response(self, request, response):
        if getattr(response, "data", False):
            data_len = len(response.data)
            if "code" not in response.data:
                response.data["code"] = '0001'

            if "requestId" not in response.data:
                response.data["requestId"] = request.id

            if "msg" not in response.data:
                response.data["msg"] = "success"

            if data_len != len(response.data):
                data = json.dumps(response.data)
                response.content = bytes(data, encoding="utf-8")
        return response
